﻿//
// Global.cs: Global fields for S9PalCommon.
//
// Copyright 2010 Michael Goethe
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using S9PalCommon.ArtistLibrary;
using log4net;

namespace S9PalCommon
{
    /// <summary>
    /// Global fields for S9PalCommon.
    /// </summary>
    public sealed class Global
    {
        #region Fields

        // Variables
        private static readonly ILog logger = log4net.LogManager.GetLogger(typeof(Global));
        private static readonly String pluginsDirectoryPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins\\";
        private static readonly String settingsDirectoryPath = AppDomain.CurrentDomain.BaseDirectory + "Settings\\";

        private static Library artistLibrary;
        private static Artist selectedArtist = null;

        // Events
        public static event EventHandler SelectedArtistChanged;

        #endregion

        #region Constructors

        static Global() {}
        Global() { }

        #endregion

        #region Properties

        /// <summary>
        /// Artist library containing information about artists, albums, etc.
        /// </summary>
        public static Library ArtistLibrary
        {
            get
            {
                if (artistLibrary == null)
                {
                    artistLibrary = new Library();
                    if (File.Exists("artistlibrary.dat"))
                    {
                        artistLibrary = S9PalCommon.Helper.DeserializeObject<Library>("artistlibrary.dat");
                    }
                }
                return artistLibrary;
            }
        }

        /// <summary>
        /// Logger that records errors and information to a log file.
        /// </summary>
        public static ILog Logger
        {
            get
            {
                return logger;
            }
        }

        /// <summary>
        /// Directory where plugins are located.
        /// </summary>
        public static String PluginsDirectoryPath
        {
            get
            {
                return pluginsDirectoryPath;
            }
        }

        /// <summary>
        /// Directory where settings files are located.
        /// </summary>
        public static String SettingsDirectoryPath
        {
            get
            {
                return settingsDirectoryPath;
            }
        }

        /// <summary>
        /// Artist currently selected by the user.
        /// </summary>
        public static Artist SelectedArtist
        {
            get 
            { 
                return selectedArtist; 
            }
            set 
            { 
                selectedArtist = value;
                OnSelectedArtistChanged();
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Called when SelectedArtist has been changed.
        /// </summary>
        private static void OnSelectedArtistChanged()
        {
            // Raise the SelectedArtistChanged event handler.
            EventHandler handler = SelectedArtistChanged;
            if (handler != null)
                handler(null, new EventArgs());
        }

        #endregion
    }
}
